/*
 * PriorityPacketQueue.h
 *
 *  Created on: 2018年9月4日
 *      Author: zhengboyuan
 */

#ifndef PRIORITYPACKETQUEUE_H_
#define PRIORITYPACKETQUEUE_H_

#include "BasicType.h"
#include "AVFramePtr.h"
#include <deque>
#include "TCriticalSection.h"
#include "TEvent.h"


namespace av
{

class PriorityPacketQueue
{
public:
	static const size_t	MAX_QUEUE_SIZE = 50 * 6;

public:
	PriorityPacketQueue();
	virtual ~PriorityPacketQueue();

	void setWaterLevel(int64_t duration);

	void setMaxSize(size_t maxSize);

	size_t size();

	bool empty();

	size_t push(AVPacketPtr& packet);

	AVPacketPtr pop();

	bool pop(AVPacketPtr& packet);

	void clear();

	bool timedwait(int ms);

	bool pop(AVPacketPtr& packet, int ms);

	bool popSize(AVPacketPtr& packet, size_t& count);
	size_t popTimeout(AVPacketPtr& packet, int ms);

	void cancelWait();

	size_t dropUntilKeyFrame();

	size_t insert(AVPacketPtr& packet);

	int64_t getDuration();

	int64_t getPosDuration();

	AVPacketPtr front();

	bool getFrontPts(int64_t& pts);

	bool getFrontPos(int64_t& pos);

	bool popEx(AVPacketPtr& packet, int ms, int mediaType);

protected:
	bool popPacket(AVPacketPtr& packet);
	int64_t getPtsDuration();

protected:
	typedef std::deque< AVPacketPtr >  PacketDeque;

	bool isVideoKey(AVPacketPtr& pkt);

protected:
	PacketDeque m_packets;
	comn::CriticalSection   m_cs;
	comn::Event	m_event;

	int64_t	m_waterLevel;
	size_t	m_maxSize;

};



} /* namespace av */

#endif /* PRIORITYPACKETQUEUE_H_ */
